package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 朱峰
 * @date 2022-1-18 8:54
 */
public class 最小时间差_539 {
    public int findMinDifference(List<String> timePoints) {
        Collections.sort(timePoints);
        int minDiff = Integer.MAX_VALUE;
        for (int i = 1; i < timePoints.size(); i++) {
            int minutePre = getMinute(timePoints.get(i - 1));
            int minuteCur = getMinute(timePoints.get(i));
            minDiff = Math.min(minDiff, minuteCur-minutePre);
        }
        int minuteFirst = getMinute(timePoints.get(0));
        int minuteLast = getMinute(timePoints.get(timePoints.size()-1));
        int diff = minuteFirst + 1440 - minuteLast;
        minDiff = Math.min(minDiff, diff);
        return minDiff;
    }

    int getMinute(String timeStr) {
        String [] time = timeStr.split(":");
        return Integer.parseInt(time[0]) * 60 + Integer.parseInt(time[1]);
    }

    @Test
    public void testFindMinDifference () {
        List<String> timePoints = new ArrayList<>();
        timePoints.add("00:30");
        timePoints.add("23:19");
        timePoints.add("00:00");
        System.out.println(findMinDifference(timePoints));
    }
}
